package cn.cjxy.userms.controller;

import cn.cjxy.userms.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器
 */
@WebFilter(urlPatterns = {"/list","/list.jsp","/findUserById","/update.jsp","/update","/add","/add.jsp","/deleleById"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取登录用户信息
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if(loginUser==null){
            //说明没有登录
            //让他回去
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
        //用户登录了  放行
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
